package modifyx

import (
	"gitee.com/zhongguo168a/gocodes/datax/listx"
)

type IPool interface {
	// 属性是否发生了变化
	IsPropChanged(key string, version int) bool
	GetNewPropInt(key string, val int) (r int, ver int)
	GetNewPropStruct(key string) (r map[string]interface{}, ver int)
	GetNewPropFloat(key string, val int) (r map[string]interface{}, ver int)
	GetNewPropBool(key string, val bool) (r bool, ver int)
}

func NewPool() (obj *Pool) {
	obj = &Pool{
		BonusList:  map[string]listx.List{},
		ModifyList: map[string]listx.List{},
		version:    map[string]int{},
	}
	return
}

type Pool struct {
	//
	Parent *Pool
	// key=结构/属性
	// 例如=CUnit/Vital/Life/Max或 CEffectDamage/Amount
	BonusList map[string]listx.List
	//
	ModifyList map[string]listx.List
	// 加成或修正发生变化, 版本号+1
	// 结构/属性
	version map[string]int
}

// key为需要修正的属性
// 例如: key=CEffectDamage/Amount
func (p *Pool) AddBonus(val IBonus, version int) {
	list := p.BonusList[val.Key()]
	list.Add(val)
	p.version[val.Key()] = version
	p.BonusList[val.Key()] = list
}

func (p *Pool) RemoveBonus(val IBonus, version int) {
	list := p.BonusList[val.Key()]
	list.Remove(val)
	p.version[val.Key()] = version
	p.BonusList[val.Key()] = list
}

// co.CModify
func (p *Pool) AddModify(val IModify, version int) {
	list := p.ModifyList[val.Key()]
	list.Add(val)
	p.version[val.Key()] = version
	p.ModifyList[val.Key()] = list
}

func (p *Pool) RemoveModify(val IModify, version int) {
	list := p.ModifyList[val.Key()]
	list.Remove(val)
	p.version[val.Key()] = version
	p.ModifyList[val.Key()] = list
}

// 增加指定键值的版本号
func (p *Pool) SetVersion(key string, version int) {
	p.version[key] = version
}

func (p *Pool) IsPropChanged(key string, version int) bool {
	if p.version[key] > version {
		return true
	}
	return false
}
